package com.cheer.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.MallCategoryProductRelationDao;
import com.cheer.dto.MallCategoryProductDTO;
import com.cheer.entity.MallCategoryProductRelationEntity;
import com.cheer.service.MallCategoryProductRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service("mallCategoryProductRelationService")
public class MallCategoryProductRelationServiceImpl extends ServiceImpl<MallCategoryProductRelationDao, MallCategoryProductRelationEntity> implements MallCategoryProductRelationService {

    @Autowired
    private MallCategoryProductRelationDao mallCategoryProductRelationDao;

    @Override
    public void add(MallCategoryProductDTO categoryProductDTO) {
        MallCategoryProductRelationEntity relation = mallCategoryProductRelationDao.selectOneByCategoryIdAndProductId(
                categoryProductDTO.getCategoryId(), categoryProductDTO.getProductId());

        if (relation == null) {
            MallCategoryProductRelationEntity insertRelation = new MallCategoryProductRelationEntity()
                    .setCategoryId(categoryProductDTO.getCategoryId())
                    .setProductId(categoryProductDTO.getProductId());
            mallCategoryProductRelationDao.insert(insertRelation);
        }
    }

    @Override
    public void delete(MallCategoryProductDTO categoryProductDTO) {
        mallCategoryProductRelationDao.deleteByCategoryIdAndProductId(categoryProductDTO.getCategoryId(),
                categoryProductDTO.getProductId());
    }

    @Override
    public void deleteByCategoryIds(List<Long> categoryIds) {
        mallCategoryProductRelationDao.deleteByCategoryIds(categoryIds);
    }

    @Override
    public void deleteByProductId(Long productId) {
        mallCategoryProductRelationDao.deleteByProductId(productId);
    }

    @Override
    public void deleteByProductIds(List<Long> productIds) {
        if(CollectionUtils.isNotEmpty(productIds)){
            mallCategoryProductRelationDao.deleteByProductIds(productIds);
        }
    }

    @Override
    public void updateSort(Long categoryId, Long productId, Long sort) {
        MallCategoryProductRelationEntity relation = new MallCategoryProductRelationEntity()
                .setSort(sort);
        mallCategoryProductRelationDao.updateByCategoryIdAndProductId(categoryId, productId, relation);
    }

    @Override
    public void updateStatusByProductId(Long productId, Boolean showStatus) {
        MallCategoryProductRelationEntity relation = new MallCategoryProductRelationEntity()
                .setShowStatus(showStatus);
        mallCategoryProductRelationDao.updateByProductId(productId, relation);
    }

    @Override
    public List<MallCategoryProductRelationEntity> listByCategoryId(Long categoryId) {
        return mallCategoryProductRelationDao.selectListByCategoryId(categoryId);
    }

    @Override
    public IPage<MallCategoryProductRelationEntity> listByCategoryId(Integer pageNum, Integer pageSize, Long categoryId) {
        return mallCategoryProductRelationDao.selectPageByCategoryId(pageNum, pageSize, categoryId);
    }

    @Override
    public List<MallCategoryProductRelationEntity> listByProductIds(List<Long> productIds) {
        if (CollectionUtils.isEmpty(productIds)) {
            return new ArrayList<>();
        }

        return mallCategoryProductRelationDao.selectListByProductIds(productIds);
    }

    @Override
    public List<MallCategoryProductRelationEntity> listByProductId(Long productId) {
        return mallCategoryProductRelationDao.selectListByProductId(productId);
    }

    @Override
    public void updateCountDownSecondByProductId(Long productId, Long countDownSecond) {

        UpdateWrapper<MallCategoryProductRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(MallCategoryProductRelationEntity::getProductId, productId)
                .set( MallCategoryProductRelationEntity::getCountDownSecond, countDownSecond );
        update(updateWrapper);

    }

    @Override
    public Long getCategoryMaxId(Long categoryId) {
        QueryWrapper<MallCategoryProductRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MallCategoryProductRelationEntity::getCategoryId, categoryId)
                .orderByDesc( MallCategoryProductRelationEntity::getSort)
                .last("limit 1");
        List<MallCategoryProductRelationEntity> list = list(queryWrapper);
        if( CollectionUtil.isEmpty(list)){
            return null;
        }
        return list.get(0).getSort();
    }

    @Override
    public void resetCategorySort(Long productId) {
        // 将存在产品列表的商品进行前置
        List<MallCategoryProductRelationEntity> mallCategoryProductRelationEntities = listByProductId(productId);
        if( CollectionUtil.isEmpty(mallCategoryProductRelationEntities)){
            return;
        }
        List<Long> categoryIds = mallCategoryProductRelationEntities.stream().map(MallCategoryProductRelationEntity::getCategoryId).distinct().collect(Collectors.toList());

        // 刷新产品所在的排序
        for (int i = 0; i < categoryIds.size(); i++) {
            // 查询每个分类的商品id
            List<MallCategoryProductRelationEntity> list = listByCategoryId(categoryIds.get(i));
            for (int j = 0; j < list.size(); j++) {
                MallCategoryProductRelationEntity mallCategoryProductRelationEntity = list.get(j);
                mallCategoryProductRelationEntity.setSort( Long.valueOf(j));
            }
            updateBatchById(list);
        }

    }

}